// Language 7 examples. (fun f (lambda x (* x x))) // f(x) = x^2 (apply f 5) // f(5) (apply f 6) // f(6) (apply f (apply f 4)) // f(f(4)) (var h f) // h is an alias for the function f (print f) // x -> x*x (print h) // x -> x*x // Language 8 examples. // We can embed lambda expressions anywhere we want. (apply (lambda x (* x x)) 5) // apply a "function literal", "anonymous lambda expression" (var f (lambda x (* x x))) // f(x) = x^2 or, f = x -> x^2 (apply f 5) // f(5) (var h (lambda y (+ 2 y))) // h(y) = 2+y or, h = x -> 2+y (print (lambda y (+ 2 y))) // y -> 2+y // Since we can embed lambda expressions anywhere we want, // we can put lambda expressions inside of lambda expressions. // Language 8 (but not Language7) allows nested functions. (fun f (lambda x (+ (* x x) (* x x x))) // A not too interesting use of nested functions. (fun f (lambda x (begin (var g (lambda x (* x x))) // nested (helper) function (var h (lambda x (* x x x))) // nested (helper) function (+ (apply g x) (apply h x)))) // An important example of how we want to use nested functions. (fun f (lambda x (begin (var g (lambda y (+ y x))) // nested function return value (closure)!!! g)))) (var f2 (apply f 3)) // f2 increments by 3 (apply f2 5) // returns 8